# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        if not head or not head.next:
            return head
        curr = head
        while curr and curr.next:
            if curr.val == curr.next.val:
                curr.next = curr.next.next
            else:
                curr = curr.next
        return head

# 工具函数：从列表构造链表
def build_linked_list(values):
    if not values: # 空列表
        return None
    head = ListNode(values[0]) # 头节点
    current = head
    for val in values[1:]:
        current.next = ListNode(val)
        current = current.next
    return head

# 工具函数：打印链表
def print_linked_list(head):
    result = []
    while head:
        result.append(head.val)
        head = head.next
    print(result)

# 测试用例
if __name__ == '__main__':
    val = [1, 1, 2, 3, 3, 4, 4, 5]
    head = build_linked_list(val)
    new_head = Solution().deleteDuplicates(head)
    print_linked_list(new_head) # [1, 2, 3, 4, 5]